package com.jp.knowledge.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.JsonObject;
import com.jp.knowledge.activity.AttentionStatisticsDetailInfo;
import com.jp.knowledge.adapter.attention.AttentionGroupStatisiticsAdapter;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.attention.Atatistics;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.view.JpDiver;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/3/20.
 */

public class AttentionStatisticsFragment extends AttentionDynamicFragment {

    private List<Atatistics> atatisticsList;
    private AttentionGroupStatisiticsAdapter adapter;

    public static AttentionStatisticsFragment newInstance(String id, int type){
        AttentionStatisticsFragment fragment = new AttentionStatisticsFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("id", id);
        bundle.putSerializable("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        atatisticsList = new ArrayList<>();
        adapter = new AttentionGroupStatisiticsAdapter(mContext, atatisticsList);
        adapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                startActivity(new Intent(mContext, AttentionStatisticsDetailInfo.class)
                        .putExtra("title", atatisticsList.get(position).getTypeName())
                        .putExtra("id", id)
                        .putExtra("type", type)
                        .putExtra("navTypeId", atatisticsList.get(position).getTypeId()));
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = super.onCreateView(inflater, container, savedInstanceState);
        contentRv.setLayoutManager(new GridLayoutManager(mContext, 2));
        contentRv.addItemDecoration(new JpDiver(mContext));
        contentRv.setAdapter(adapter);
        return contentView;
    }

    @Override
    protected void getData(int page, int model){
        JsonObject param = new JsonObject();
        param.addProperty("type", type);
        param.addProperty("id", id);
        param.addProperty("page", page);
        HttpHelper.getInstance(context).attentionGroupStatistics(param, model, this);
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            onError(requestType);
        } else if(requestType == REFRESH_CODE || requestType == LOADMORE_CODE){
            if(requestType == REFRESH_CODE)
                atatisticsList.clear();
            List<Atatistics> atatisticses = data.getList(Atatistics.class);
            if(atatisticses != null && atatisticses.size() > 0)
                atatisticsList.addAll(atatisticses);
            adapter.updateAdapter(atatisticsList);
        }
    }
}
